<?php

declare (strict_types=1);
namespace App\Model;

use Hyperf\DbConnection\Model\Model;
use Hyperf\Database\Model\Builder;
use Hyperf\Database\Schema\Schema;

class RechargeAmount extends Model
{
    
    use SelectTrait;

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'recharge_amount';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $guarded=['id'];
    protected $hidden = ['is_del'];

    protected $casts = [
        'add_time'=>'datetime:Y-m-d H:i:s',
        'update_time'=>'datetime:Y-m-d H:i:s',
    ];

    const CREATED_AT = 'add_time';
    const UPDATED_AT = 'update_time';

    public $timestamps = true;

    protected function boot(): void
    {   
        $this->bootTraits();
        $columns = Schema::getColumnListing($this->table);
        $this->guarded+=$columns;
        unset($this->guarded['id']);
        //model 软删除过滤 避免每次查询都添加is_del条件 去除条件添加withoutGlobalScope
        $this->addGlobalScope('is_del', function(Builder $builder) {
            $builder->where($this->table.'.is_del',1);
        });
        // var_dump($columns);
    }

    protected $dateFormat = 'U';


    
    public function type(){
        return $this->hasOne(RechargeType::class,'id','type_id');
    }
    
    //映射status
    public function getStatusMappingAttribute(){
       
        switch($this->attributes['status']){
            case 1:$val='正常';break;
            case 2:$val='禁用';break;
            default:$val='';break;
        }
        return $val;
    }
    
}